package org.inspursc.s0517.health.common.service.impl;

import org.inspursc.s0517.health.common.converter.OrgConverter2OrgDTO;
import org.inspursc.s0517.health.common.dao.SysOrganizationDao;
import org.inspursc.s0517.health.common.dto.InsAssessmentNumDTO;
import org.inspursc.s0517.health.common.dto.SysOrganizationDTO;
import org.inspursc.s0517.health.common.entity.SysOrganization;
import org.inspursc.s0517.health.common.service.SysOrganizationService;
import org.inspursc.s0517.health.evaluation.dto.InsSurveyAnswerDTO;
import org.inspursc.s0517.health.evaluation.service.InsSurveyAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author B_Dream
 * @date 2018/6/7 14:04
 */
@Service
public class SysOrganizationServiceImpl implements SysOrganizationService {


    @Autowired
    SysOrganizationDao organizationDao;

    @Autowired
    InsSurveyAnswerService insSurveyAnswerService;

    @Override
    public SysOrganizationDTO save(SysOrganizationDTO sysOrgDTO) {
        try {
            SysOrganization sysOrg=  OrgConverter2OrgDTO.converter(sysOrgDTO);
            SysOrganization sysOrgNew= organizationDao.save(sysOrg);
            return OrgConverter2OrgDTO.converter(sysOrgNew);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean deleteById(Integer id) {
        try {
            organizationDao.deleteById(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public SysOrganizationDTO findOneById(Integer id) {
        try {
            SysOrganization org = organizationDao.getOne(id);
            if (org != null) {
                SysOrganizationDTO dto = OrgConverter2OrgDTO.converter(org);
                return dto;
            } else {
                return null;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public SysOrganizationDTO findOneByDomainName(String domainName) {
        SysOrganization org=organizationDao.findByDomainName(domainName);

        if(org!=null) {
            SysOrganizationDTO dto = OrgConverter2OrgDTO.converter(org);
            return dto;
        }else {
            return null;
        }
    }
    //for change date format
    public String getNowDateShort(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

    @Override
    public List<SysOrganizationDTO> findAll() {


        List<SysOrganization> orgList = organizationDao.findAll();

        List<SysOrganizationDTO> dtoList = orgList.stream().map((e) -> OrgConverter2OrgDTO.converter(e)).collect(Collectors.toList());
       // List<SysOrganizationDTO> dtoList = orgList.stream().map(OrgConverter2OrgDTO::converter).collect(Collectors.toList());
            for(SysOrganizationDTO orgDTO : dtoList){
                List<InsSurveyAnswerDTO> insSurveyAnswerDTOS = insSurveyAnswerService.findAllByOrganizationId(orgDTO.getId());
                List<InsAssessmentNumDTO> assessmentNumDTOList = new ArrayList<>();
                for(InsSurveyAnswerDTO surveyAnswerDTO : insSurveyAnswerDTOS){
                   InsAssessmentNumDTO insAssessmentNumDTO = new InsAssessmentNumDTO();

                   insAssessmentNumDTO.setSurveyAnswerId(surveyAnswerDTO.getId());
                   insAssessmentNumDTO.setSurveyId(surveyAnswerDTO.getSurveyId());
                   insAssessmentNumDTO.setSurveyName(surveyAnswerDTO.getSurveyName());
                   insAssessmentNumDTO.setDateTime(getNowDateShort(surveyAnswerDTO.getDateTime()));

                   //add to assesmentNumDTOList
                    assessmentNumDTOList.add(insAssessmentNumDTO);
                }
                orgDTO.setInsAssessment(assessmentNumDTOList);
            }

        return dtoList;
    }
}
